package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jx.dao.CatDao;
import com.jx.dao.ProductDao;
import com.jx.daoimpl.CatDaoImpl;
import com.jx.daoimpl.ProductDaoImpl;
import com.jx.service.ProService;
import com.jx.servicrimpl.ProServiceImpl;

public class ShowProController extends HttpServlet{
		ProService showproservice = new ProServiceImpl();
		ProductDao productdao =new ProductDaoImpl();
		CatDao catdao = new CatDaoImpl();
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			// TODO Auto-generated method stub
			doPost(req, resp);
		}
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			//设置字符集
			req.setCharacterEncoding("UTF8");
			resp.setCharacterEncoding("UTF8");
			//创建session对象
			HttpSession session =req.getSession();

			//接受前台传来的数据
			int page = Integer.parseInt(req.getParameter("page"));
			int pageSize = Integer.parseInt(req.getParameter("rows"));
			int start = (page-1)*pageSize;
			String pname = req.getParameter("proname");
			//将查出的商品分类对象数组放入到session中
			session.setAttribute("product",productdao.showProduct(start, pageSize));
			session.setAttribute("category", catdao.showCatrgory(0, 50));
			//创建流用以向前台写回数据
			PrintWriter pw = resp.getWriter();
			if (pname==null) {
				pw.write(showproservice.showProductService(start,pageSize).toString());
			}else {
				pw.write(showproservice.showProductService(pname, start, pageSize).toString());
			}
		}
}
